//
//  MainTabBar.swift
//  Sinnaweibo模拟项目
//
//  Created by 尹家鑫 on 16/3/6.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    //自定义TabBar 需要将之前的每一个tabbarbutton 的大小减少，并且将中间位置空出来
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //uitabbarbutton是私有的类
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        var index: CGFloat = 0
        
        //定义一个递增的索引
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subView.frame = CGRectOffset(rect, index * w, 0)
                
                index += (index == 1 ? 2 : 1)
            }
            
        }
        
        //添加子视图
        addSubview(plusBtn)
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    //MARK 懒加载视图
    lazy var plusBtn: UIButton = {
        
        //UIButton 自定义样式的按钮
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //自适应大小
        btn.sizeToFit()
        
        return btn
    
    }()
}
